﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Expression;
using System.Collections;


namespace Model
{
    public class Processo
    {

        #region Propriedades

        string idProcesso;
        string nrProcesso;
        string tipoProcesso;
        Foro foro;
        Cliente cliente;
        char status;
        DateTime data;
        string pasta_local;
        decimal valor_causa;
        string protocolo;
        string observacoes_detalhes;

        public string IdProcesso
        {
            get { return idProcesso; }
            set { idProcesso = value; }
        }
       

        public string NrProcesso
        {
            get { return nrProcesso; }
            set { nrProcesso = value; }
        }
        

        public string TipoProcesso
        {
            get { return tipoProcesso; }
            set { tipoProcesso = value; }
        }
        

        public Foro Foro
        {
            get { return foro; }
            set { foro = value; }
        }

        public Cliente Cliente
        {
            get { return cliente; }
            set { cliente = value; }
        }

        

        public char Status
        {
            get { return status; }
            set { status = value; }
        }
        

        public DateTime Data
        {
            get { return data; }
            set { data = value; }
        }
        

        public string Pasta_local
        {
            get { return pasta_local; }
            set { pasta_local = value; }
        }
       

        public decimal Valor_causa
        {
            get { return valor_causa; }
            set { valor_causa = value; }
        }
        

        public string Protocolo
        {
            get { return protocolo; }
            set { protocolo = value; }
        }
        

        public string Observacoes_detalhes
        {
            get { return observacoes_detalhes; }
            set { observacoes_detalhes = value; }
        }

        #endregion

        #region Construtores

        public Processo()
        {
        }

        public Processo(string pIdprocesso, string pNrProcesso, string pTipoProcesso,
            Foro pForo, Cliente pCliente, char pStatus, DateTime pData, string pPasta_Local,
            decimal pValor_Causa, string pProtocolo, string pObservacoes_Detalhes)
        {
            IdProcesso = pIdprocesso;
            NrProcesso = pNrProcesso;
            TipoProcesso = pTipoProcesso;
            Foro = pForo;
            Cliente = pCliente;
            Data = pData;
            Pasta_local = pPasta_Local;
            Valor_causa = pValor_Causa;
            Protocolo = pProtocolo;
            Observacoes_detalhes = pObservacoes_Detalhes;
        }

        #endregion


        #region Metodos

        #region Persist
        public Boolean Persist()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Save(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Update
        public Boolean Update()
        {
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objSession.Update(this);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region Delete
        public static Boolean Delete(string pIdProcesso)
        {
            Processo objProcesso = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objProcesso = (Processo)objSession.Load(typeof(Model.Processo), pIdProcesso);
                objSession.Update(objProcesso);
                objTransaction.Commit();
                objSession.Close();
                return true;
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
        }
        #endregion

        #region RetriveObject
        public static Processo RetriveObject(string pNrProcesso)
        {
            Processo objProcesso = null;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                objProcesso = (Processo)objSession.Load(typeof(Model.Processo), pNrProcesso);
                objSession.Close();
            }
            catch (Exception ex)
            {
                objTransaction.Rollback();
                objSession.Close();
                throw ex;
            }
            return objProcesso;
        }

        #endregion

        #region RetriveObjects
        public static IList RetriveObjects()
        {
            IList listProcessos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Processo));
                criteria.AddOrder(Order.Asc("NrProcesso"));

                listProcessos = criteria.List();

                objSession.Close();
            }
            catch (Exception ex)
            {
                listProcessos = null;
                objTransaction.Rollback();
                throw ex;
            }
            return listProcessos;
        }

        public static IList RetriveObjects(string pNrProcesso)
        {
            IList listProcessos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Processo));
                criteria.Add(Expression.Like("NrProcesso", pNrProcesso))
                        .AddOrder(Order.Asc("NrProcesso"));
                listProcessos = criteria.List();
                objSession.Close();

            }
            catch (Exception ex)
            {
                listProcessos = null;
                objTransaction.Rollback();
                throw ex;
            }
            return listProcessos;
        }

        public static IList RetriveObjects(Cliente pCliente)
        {
            IList listProcessos;
            ISession objSession = ConnectionFactory.getConnection().OpenSession();
            ITransaction objTransaction = objSession.BeginTransaction();

            try
            {
                ICriteria criteria = objSession.CreateCriteria(typeof(Model.Processo));
                criteria.Add(Expression.Like("Cliente", pCliente))
                        .AddOrder(Order.Asc("NrProcesso"));
                listProcessos = criteria.List();
                objSession.Close();

            }
            catch (Exception ex)
            {
                listProcessos = null;
                objTransaction.Rollback();
                throw ex;
            }
            return listProcessos;
        }

        #endregion

        #endregion

    }
}
